
package doc2db.interfaces.action.createTable;

import doc2db.DbManager.DbSelects;
import doc2db.Keey;
import doc2db.UserProp;
import doc2db.interfaces.action.ShowTableAction;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

/**
 *
 * @author yura
 */
public class CreateTableAction implements ActionListener{
    JEditorPane sql;
    MyJEditorPane info;

    public CreateTableAction(JEditorPane sql, MyJEditorPane info) {
        this.info = info;
        this.sql = sql;
    }

    public void actionPerformed(ActionEvent event) {
        if (!Keey.isConnected) {
            return;
        }

        final DbClientDialogCreateTable createTable = new DbClientDialogCreateTable(null);
        createTable.ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = createTable.getTableSql();
                DbSelects query;
                try {
                    UserProp up = new UserProp();
                    query = new DbSelects(up.getURL(), up.getUserProp());
                    if (query.sendQueryUpdate(str)) {
                        JOptionPane.showMessageDialog(null, "запрос выполнен",
                                "сообщение", JOptionPane.INFORMATION_MESSAGE);
                        createTable.setVisible(false);
                        
                        String str2 = createTable.getAdditionalInformation();
                        if(! str2.trim().equals("")){
                            query.sendQueryUpdate(str2);
                        }
                        sql.setText(sql.getText() + "\n" + str + "\n" + str2);
                        
                        ShowTableAction showTable = new ShowTableAction(info, sql);
                        showTable.res = createTable.getTableName();
                        showTable.actionPerformed(null);
                    } else {
                        JOptionPane.showMessageDialog(null, "запрос НЕ выполнен",
                                "ошибка", JOptionPane.ERROR_MESSAGE);
                        sql.setText(sql.getText() + "\n" + str);
                    }
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "" + ex,
                            "ошибка", JOptionPane.ERROR_MESSAGE);
                    sql.setText(sql.getText() + "\n" + str);
                } catch (ClassNotFoundException ex) {
                    JOptionPane.showMessageDialog(null, "" + ex,
                            "ошибка", JOptionPane.ERROR_MESSAGE);
                    sql.setText(sql.getText() + "\n" + str);
                }
            }
        });
        createTable.setVisible(true);
    }


}
